import requests
import json
import matplotlib.pyplot as plt  
import time
rebot_url="https://xz.wps.cn/api/v1/webhook/send?key=f4b5d2acf9f0150a3e507e8797369018"
# rebot_url="https://xz.wps.cn/api/v1/webhook/send?key=81a49ba918b8a56b1eeef56bde3349fa"
perfeyeId="670bc8f14d6921530416f009"
def rebotSendMessage(rebotMessage):
    global rebot_url
    payload = json.dumps({
    "msgtype": "markdown",
    "markdown": {
        "text": rebotMessage
    }
    })
    headers = {
    'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
    'Content-Type': 'application/json',
    'Accept': '*/*',
    'Host': 'xz.wps.cn',
    'Connection': 'keep-alive'
    }
    response = requests.request("POST", rebot_url, headers=headers, data=payload)
    if response.text=='{"result":"ok"}':
        print("请求成功")
    else:
        print(f"请求失败,原因是{response.text}")

def getImageList(data):
    imageList=[]
    print(data)
    if 'data' not in data:
        print('请求失败')
    elif 'ImageList' not in data['data']:
        print('没有找到ImageList')
    else:
        for item in data['data']['ImageList']:
            imageList.append(item)
    return imageList
def getFpsList(data):
    fpsList=[]
    if 'data' not in data:
        print('请求失败')
    elif 'DataList' not in data['data']:
        print('没有找到DataList')
    else:
        for item in data['data']['DataList']:
            fpsList.append(round(item['FPS'],2))
    return fpsList
def format_time(seconds):  
    # 计算小时数和分钟数  
    minutes, seconds = divmod(seconds, 60)  
    hours, minutes = divmod(minutes, 60)  
      
    # 格式化小时和分钟为两位数字  
    
    formatted_hours = f"{hours:02d}"  
    formatted_minutes = f"{minutes:02d}"  
    formatted_second=f"{seconds:02d}"
    # 拼接成 "HH:MM" 格式的字符串  
    if formatted_hours=="00":
        formatted_hours=""
    else:
        formatted_hours+=":"
    return f"{formatted_hours}{formatted_minutes}:{formatted_second}"  
import matplotlib.pyplot as plt  
  
def plot_line_chart(y_values):  
    # 假设X值是连续的整数，从0秒开始，每个点代表1秒  
    x_values = list(range(len(y_values)))  
      
    # 创建一个图形和一个子图（这一步其实是可选的，但为了代码清晰和可能的后续扩展，这里保留）  
    plt.figure()  
      
    # 绘制折线图  
    plt.plot(x_values, y_values, marker='o')  # marker='o' 表示在每个数据点上绘制一个圆圈  
    
     # 设置Y轴从0开始  
    plt.ylim(bottom=0)  

    # 设置X轴刻度，每10秒标注一次  
    # 首先，计算需要标注的刻度位置（即0, 10, 20, ...）  
    tick_positions = [i for i in x_values if i % 10 == 0]  
    # 然后，为这些位置生成对应的标签  
    tick_labels = [f'{format_time(i)}' for i in tick_positions]  
    # 设置X轴刻度位置和标签  
    plt.xticks(tick_positions, tick_labels)  
      
    # 添加标题和标签  
    plt.title('FPS')  
    plt.xlabel('Time')  # 修改X轴标签为时间（秒）  
    plt.ylabel('FPS')  
      
    # 显示网格线  
    plt.grid(True)  
      
    # 显示图形  
    plt.show()

url = f"https://perfeye.console.testplus.cn/api/show/task/{perfeyeId}?email=lujiahao@kingsoft.com"

payload = ""
headers = {
   'Cookie': 'mysession=MTcyODUyNzU2NHxOd3dBTkRkUlVsbEVSMU5NVlZSQ05GUlRObGROTnpSRlRrWklURkJJV0ZrMFVrVllObGRPVEZKQk5UWkxSbFpZVGt4UlNFNUxTVkU9fFH63fQciHC2eEPg9QdqLuctFF85NLphf0cSCG1VZEO8',
   'Content-Type': 'text/plain',
   'Host': 'perfeye.console.testplus.cn'
}
response = requests.request("POST", url, headers=headers, data=payload)
data = response.json() 
# imageList=getImageList(data)
fpsList=getFpsList(data)
i=0
message=''
# 图片
# for item in imageList:
#     if item=='':
#         i+=1
#         continue
#     print(f'第【{i}】秒截图:{item}')
#     message+=f'![第{i}秒截图]({item})<br>'
#     if len(message)>4000:
#         rebotSendMessage(message)
#         message=''
#     i+=1
#     if i%20==0:
#         time.sleep(1)

# Fps
# for item in fpsList:
#     print(f'【{format_time(i)}】:{item}')
#     message+=f'【{format_time(i)}】:{item}<br>'
#     if len(message)>4000:
#         rebotSendMessage(message)
#         message=''
#     i+=1
# rebotSendMessage(message)
plot_line_chart(fpsList)